﻿using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace AppEszköz.Xml
{
    public sealed class XmlHelper
    {
        private XmlHelper()
        {
        }

        public static string Serialize(object obj)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");
            XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
            MemoryStream memoryStream = new MemoryStream();
            xmlSerializer.Serialize((Stream)memoryStream, obj);
            return new UTF8Encoding().GetString(memoryStream.ToArray());
        }

        public static object Deserialize(string xml, Type type)
        {
            return new XmlSerializer(type).Deserialize((Stream)new MemoryStream(new UTF8Encoding().GetBytes(xml)));
        }
    }
}
